package com.wxq.draw;

import java.util.List;
import java.util.Map;
import com.wxq.schoolmap.R;
import android.content.Context;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class MapLayout extends RelativeLayout {

	//地图UI
	protected DrawView drawView;//wxq 的地图显示SDK，是继承自veiw的图层结构
	public DrawrouteVeiw routeView;//路线显示，也是继承自veiw的图层结构
	//浮层
	protected ImageView overlayIV;//点击时出现在小图钉
	protected ImageView routefrom,routegoto;//起点与终点图标
	public ImageView[] sitepoint=new ImageView[num];//多处打点相关
	protected LinearLayout overlayPopupView;//选择起点终点浮窗
	protected LinearLayout overlayPopupView2;//查询路径浮窗
	protected LinearLayout overmenuView;//公共设施浮层
	protected LinearLayout routeplaybutton;//路径导航按钮浮层
	protected LinearLayout routeinfo;//路径说明浮窗
	protected PointF overlayScreenCoord,overlayMapCoord;//选择起点终点浮窗坐标
	protected PointF overlayScreenCoord2,overlayMapCoord2;//查询路径浮窗坐标
	protected PointF routeinfoScreenCoord,routeinfoMapCoord;//路径说明浮窗坐标
	public PointF[] sitepiontMapCoord=new PointF[num],sitepointScreenCoord=new PointF[num];//多处打点各个坐标
	protected String currentSpotId = "";//当前图钉所打的点spotid
	protected String spotName = "";//当前图钉所打的点名称
	protected int currentpiont=-1;//当前的路径导航位置
	protected PointF fromScreenCoord,fromMapCoord,gotoScreenCoord,gotoMapCoord;//起点与终点的地图与屏幕坐标
	public static final int num=7;//多处打点的最大数目
	//-1是初始化，这是不管点向左或右都显示第一个
	
	//手势解码器
	protected GestureDetector gestureDetector = new GestureDetector(new LayoutGestureDetector());
	protected ScaleGestureDetector mScaleDetector = new ScaleGestureDetector(getContext(), new ScaleListener());
	
	//------------------构造函数-------------------------
	public MapLayout(Context context) {
		super(context);
		initChildViews();
	}

	public MapLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		initChildViews();
	}
	//--------------------初始化--------------------------
	//初始化所有孩子View
	protected void initChildViews() {
		initDrawView();
		initOverlay();
	}
	
	//初始化地图View
	protected void initDrawView() {
		drawView = new DrawView(getContext());
		addView(drawView);
		drawView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
		routeView = new DrawrouteVeiw(getContext());
		addView(routeView);
	}
	
	public void loaingSchoolMap() {
		drawView.loadData("Floor0");
		 PointF pF=new PointF();
			 pF.x=332;pF.y=588;
			 scrolltopoint(pF);
	}
	
	public void close() {
		drawView.close();
	}
	
	//------------------覆写的父类函数-------------------------------
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		drawView.measure(widthMeasureSpec, heightMeasureSpec);
		routeView.measure(widthMeasureSpec, heightMeasureSpec);////
		
		overlayIV.measure(View.MeasureSpec.makeMeasureSpec(40 , View.MeasureSpec.EXACTLY),View.MeasureSpec.makeMeasureSpec(40, View.MeasureSpec.EXACTLY));
		routegoto.measure(View.MeasureSpec.makeMeasureSpec(40 , View.MeasureSpec.EXACTLY),View.MeasureSpec.makeMeasureSpec(40, View.MeasureSpec.EXACTLY));
		routefrom.measure(View.MeasureSpec.makeMeasureSpec(40 , View.MeasureSpec.EXACTLY),View.MeasureSpec.makeMeasureSpec(40, View.MeasureSpec.EXACTLY));
		for(int i=0;i<num;i++){
		     sitepoint[i].measure(View.MeasureSpec.makeMeasureSpec(40 , View.MeasureSpec.EXACTLY),View.MeasureSpec.makeMeasureSpec(40, View.MeasureSpec.EXACTLY));
		}
        overlayPopupView.measure(View.MeasureSpec.makeMeasureSpec(320, View.MeasureSpec.AT_MOST),View.MeasureSpec.makeMeasureSpec(60, View.MeasureSpec.AT_MOST));
        overlayPopupView2.measure(View.MeasureSpec.makeMeasureSpec(320, View.MeasureSpec.AT_MOST),View.MeasureSpec.makeMeasureSpec(60, View.MeasureSpec.AT_MOST));
        overmenuView.measure(View.MeasureSpec.makeMeasureSpec(320, View.MeasureSpec.AT_MOST),View.MeasureSpec.makeMeasureSpec(60, View.MeasureSpec.AT_MOST));	
        routeinfo.measure(View.MeasureSpec.makeMeasureSpec(320, View.MeasureSpec.AT_MOST),View.MeasureSpec.makeMeasureSpec(60, View.MeasureSpec.AT_MOST));	
        routeplaybutton.measure(View.MeasureSpec.makeMeasureSpec(320, View.MeasureSpec.AT_MOST),View.MeasureSpec.makeMeasureSpec(60, View.MeasureSpec.AT_MOST));	
        //this.measure(View.MeasureSpec.makeMeasureSpec(320, View.MeasureSpec.AT_MOST),View.MeasureSpec.makeMeasureSpec(60, View.MeasureSpec.AT_MOST));
       
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);
		drawView.layout(l, t, r, b);
		routeView.layout(l, t, r, b);
		moveOverlayTo();
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// 处理触摸事件；1.使得mapUI可以拖动；2.使得点击可添加浮层；3.浮层可随mapUI一起拖动
		//1.由布局类来控制
		//2.同上
		mScaleDetector.onTouchEvent(event);
		gestureDetector.onTouchEvent(event);
		 
		return true;
	}
	
	//-------------scale手势解析器--------------
	class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
        @Override
        public boolean onScale(ScaleGestureDetector detector) {
        	
            float factor = detector.getScaleFactor();
            if (factor > 1.05f && factor < 1.3f) {
				zoomOut();
			}
            else if (factor < 0.97f && factor > 0.75) {
				zoomIn();
			}
            return true;
        }
    }
	
	//----------------手势解析器----------------------------
	class LayoutGestureDetector extends GestureDetector.SimpleOnGestureListener
	 {		
		@Override
		public boolean onSingleTapUp(MotionEvent e) {
			// 取得点击位置,并立即转化成地图坐标
			PointF pF = new PointF(e.getX(), e.getY());
			Log.d("lxr", "X = "+e.getX()+";    Y = "+e.getY());
			pinAtMapWithScreenCoord(pF);
			hidemenu();
			return super.onSingleTapUp(e);
		}

		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2,float distanceX, float distanceY) 
		{
			super.onScroll(e1, e2, distanceX, distanceY);
			if (e2.getAction() == MotionEvent.ACTION_MOVE && e2.getPointerCount() == 1)
			{
				drawView.scrollByDistance(distanceX, distanceY);
				routeView.scrollByDistance(distanceX, distanceY);
				overlayScrollByDistance(distanceX,distanceY);
						
			}
			return true;
		} 
		
	 }
	
	//----------------坐标转换------------------------
	protected PointF getScreenCoordinateFromMap(PointF mapCoord) {
		PointF screenCoord = new PointF();
		//计算对应的屏幕坐标
		float orgX = mapCoord.x;
		float orgY = mapCoord.y;
		float scale = drawView.scale;
		float offsetX = drawView.xOffset;
		float offsetY = drawView.yOffset;
		Matrix scaleMatrix = new Matrix();
		scaleMatrix.setScale(scale, scale);
		float [] point = new float []{orgX,orgY};
		scaleMatrix.mapPoints(point);
		screenCoord.x = point[0]+offsetX;
		screenCoord.y = point[1]+offsetY;
		return screenCoord;
	}	
	
	protected PointF getMapCoordinateFromScreen(PointF screenCoord) {
		PointF mapCoord = new PointF();
		//计算对应的地图坐标
		
		float scale = drawView.scale;
		float offsetX = drawView.xOffset;
		float offsetY = drawView.yOffset;
		float orgX = screenCoord.x - offsetX;
		float orgY = screenCoord.y - offsetY;
		Matrix scaleMatrix = new Matrix();
		scaleMatrix.setScale(1/scale, 1/scale);
		float [] point = new float []{orgX,orgY};
		scaleMatrix.mapPoints(point);
		mapCoord.x = point[0];
		mapCoord.y = point[1];
		return mapCoord;
	}
	
	//-------------------------浮层相关------------------------
	protected void initOverlay() {
		routeinfo = (LinearLayout) ((LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.routeinfo, null);
		addView(routeinfo);
		overlayPopupView = (LinearLayout) ((LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.draw_overlay_bar, null);
		addView(overlayPopupView);
		overlayPopupView2 = (LinearLayout) ((LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.draw_overlay_bar2, null);
		addView(overlayPopupView2);
		
		overlayIV = new ImageView(getContext());
		overlayIV.setImageResource(R.drawable.geo_point_red);
		addView(overlayIV);
		
		
		for(int i=0;i<num;i++){
		sitepoint[i] = new ImageView(getContext());
		//sitepoint[i].setImageResource(R.drawable.da_marker_red);
		addView(sitepoint[i]);}
		
		
		routefrom = new ImageView(getContext());
		routefrom.setImageResource(R.drawable.routefrom);
		addView(routefrom);
	
		
		routegoto = new ImageView(getContext());
		routegoto.setImageResource(R.drawable.routyegoto);
		addView(routegoto);
		
		
		
		overmenuView = (LinearLayout) ((LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.draw_overlay_menu, null);
		addView(overmenuView);
		routeplaybutton = (LinearLayout) ((LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.routeplaybottom, null);
		addView(routeplaybutton);

		
		overlayScreenCoord = new PointF();
		overlayMapCoord = getMapCoordinateFromScreen(overlayScreenCoord);
		overlayScreenCoord2 = new PointF();
		overlayMapCoord2 = getMapCoordinateFromScreen(overlayScreenCoord2);
		fromScreenCoord = new PointF();
		fromMapCoord = getMapCoordinateFromScreen(fromScreenCoord);
		gotoScreenCoord = new PointF();
		gotoMapCoord = getMapCoordinateFromScreen(gotoScreenCoord);
		for(int i=0;i<num;i++){
		    sitepointScreenCoord[i] = new PointF();
		    sitepiontMapCoord[i] = getMapCoordinateFromScreen(sitepointScreenCoord[i]);
		}
		routeinfoScreenCoord = new PointF();
		routeinfoMapCoord = getMapCoordinateFromScreen(routeinfoScreenCoord);
		hideAll();
	}
	
	//显示浮层图标
	protected void showOverlay()
	{
		
		if(overlayPopupView.getVisibility() != View.VISIBLE){
			overlayPopupView.setVisibility(View.VISIBLE);
		}
		if (overlayIV.getVisibility() != View.VISIBLE) {
			overlayIV.setVisibility(View.VISIBLE);
		}
	}
	
	public void showOverlay2(PointF pointF)
	{
		overlayMapCoord2 = pointF;
		overlayScreenCoord2 = getScreenCoordinateFromMap(pointF);
		if(overlayPopupView2.getVisibility() != View.VISIBLE){
			overlayPopupView2.setVisibility(View.VISIBLE);
		}
		invalidate();	//移动浮层到正确位置
		requestLayout();
	}
	
	protected void showfrom()
	{
		if (routefrom.getVisibility() != View.VISIBLE) {
			routefrom.setVisibility(View.VISIBLE);
		}
		
	}
	
	protected void showgoto()
	{
		if (routegoto.getVisibility() != View.VISIBLE) {
			routegoto.setVisibility(View.VISIBLE);
		}
		
	}
	
	public void showsitepoint(int i)
	{		
		 if(sitepoint[i].getVisibility() != View.VISIBLE)
			sitepoint[i].setVisibility(View.VISIBLE);
	}

	public void showrouteinfo()
	{
		Log.d("lxr", "showrouteinfo:"+currentpiont);	
		TextView titleView = (TextView) routeinfo.findViewById(R.id.routeinfo);
		String spotName = routeView.piontlist.get(currentpiont).get("pionttext").toString();
		Log.d("lxr", "spotName:"+spotName);	
		if (!spotName.isEmpty()) {
			titleView.setText(spotName);
		}else{titleView.setText("未知区域");}
		float xx=Float.parseFloat(routeView.piontlist.get(currentpiont).get("Coord_X").toString());
		float yy=Float.parseFloat(routeView.piontlist.get(currentpiont).get("Coord_Y").toString());
		PointF pointF = new PointF(xx,yy);
		routeinfoMapCoord = pointF;
		routeinfoScreenCoord = getScreenCoordinateFromMap(pointF);
		if(routeinfo.getVisibility() != View.VISIBLE)
			routeinfo.setVisibility(View.VISIBLE);
		   invalidate();
		   requestLayout();
		   scrolltopoint(routeinfoMapCoord);
	}
	
	//lxr------显示浮层菜单
	public void showmenu(final Context context)
	{
		if(overmenuView.getVisibility() != View.VISIBLE)
		{overmenuView.setVisibility(View.VISIBLE);}
		else{overmenuView.setVisibility(View.INVISIBLE);}
		invalidate();	//移动浮层到正确位置
		requestLayout();

	}
	

	//设置浮层弹出内容
	protected void setOverlayInfo() {
		//获得地图某点信息
			TextView titleView = (TextView) overlayPopupView.findViewById(R.id.titleView);
			String spotName = getSpotInfoAtMapCoord(overlayMapCoord);
			if (!TextUtils.isEmpty(spotName)) {
				titleView.setText(spotName);
			}
			else {
				titleView.setText("未知区域");
			}
	}

	//移动浮层位置.
	protected void moveOverlayTo() {
		
		int overlayIVWidth = (int)(overlayIV.getWidth()*0.4);
		int overlayIVHeight = (int)(overlayIV.getHeight()*0.4);
		int overlayIVLeft = (int) (overlayScreenCoord.x - overlayIVWidth/2);
		int overlayIVRight = (int) (overlayScreenCoord.x + overlayIVWidth/2);
		int overlayIVTop = (int) (overlayScreenCoord.y - overlayIVHeight);
		int overlayIVBottom = (int) (overlayScreenCoord.y);
				
		int popupWidth = overlayPopupView.getWidth();
		int popupHeight = overlayPopupView.getHeight();
		int popupLeft = (int) (overlayScreenCoord.x - popupWidth/2 );
		int popupRight = (int) (overlayScreenCoord.x + popupWidth/2) ;
		int popupTop = overlayIVTop - popupHeight+12;
		int popupBottom = overlayIVTop+12;
		
		overlayPopupView.layout(popupLeft,popupTop,popupRight,popupBottom);
		overlayIV.layout(overlayIVLeft,overlayIVTop,overlayIVRight,overlayIVBottom);
		
		int popupWidth2 = overlayPopupView2.getMeasuredWidth();
		int popupHeight2 = overlayPopupView2.getMeasuredHeight();
		int popupLeft2 = (int) (overlayScreenCoord2.x - popupWidth2 / 2);
		int popupRight2 = (int) (overlayScreenCoord2.x + popupWidth2 /2) ;
		int popupTop2 = (int) (overlayScreenCoord2.y) - popupHeight2-100;
		int popupBottom2 = (int) (overlayScreenCoord2.y)-100;
		overlayPopupView2.layout(popupLeft2,popupTop2,popupRight2,popupBottom2);
		
		//lxr--
		
		routefrom.layout((int)fromScreenCoord.x-30,(int)fromScreenCoord.y-80,
	               (int)fromScreenCoord.x+30,(int)fromScreenCoord.y);
		routegoto.layout((int)gotoScreenCoord.x-30,(int)gotoScreenCoord.y-80,
	               (int)gotoScreenCoord.x+30,(int)gotoScreenCoord.y);
		
		int siteWidth = sitepoint[0].getMeasuredWidth()*2;
		int siteHeight = sitepoint[0].getMeasuredHeight()*2;
		for(int i=0;i<num;i++){
			int siteLeft = (int) (sitepointScreenCoord[i].x - siteWidth /2);
			int siteRight = (int) (sitepointScreenCoord[i].x + siteHeight /2) ;
			int siteTop = (int) (sitepointScreenCoord[i].y - siteHeight);
			int siteBottom = (int) (sitepointScreenCoord[i].y);
			sitepoint[i].layout(siteLeft,siteTop,siteRight,siteBottom);
		}		
		routeinfo.layout((int)routeinfoScreenCoord.x-140,(int)routeinfoScreenCoord.y-150,
	               (int)routeinfoScreenCoord.x+140,(int)routeinfoScreenCoord.y);		
		overmenuView.layout(300+this.getLeft(),this.getBottom()-350,300+this.getLeft()+overmenuView.getMeasuredWidth(),this.getBottom()-100);
		//overfloormenuView.layout(0,this.getBottom()-360,overfloormenuView.getMeasuredWidth(),this.getBottom()-53);
		
		int a=(this.getMeasuredWidth())/2;
		int b=(this.getMeasuredHeight())/2;
		int c=(routeplaybutton.getWidth())/2;
		int d=(routeplaybutton.getHeight())/2;
		routeplaybutton.layout(a-c,b-d,a+c,b+d);
		
	}
	
	//在屏幕某个地方打点，根据屏幕坐标
	public void pinAtMapWithScreenCoord(PointF pointF) {
		overlayScreenCoord = pointF;
		overlayMapCoord = getMapCoordinateFromScreen(overlayScreenCoord);
		showOverlay();//显示浮层
		setOverlayInfo();//设置并测量浮层栏
		invalidate();//移动浮层到正确位置
		requestLayout();
	}
	
	//在屏幕某个地方打点，根据地图坐标
	
	public void pinAtMapWithMapCoord(PointF pointF) {
		overlayMapCoord = pointF;
		overlayScreenCoord = getScreenCoordinateFromMap(pointF);
		showOverlay();//显示浮层
		setOverlayInfo();//设置并测量浮层栏
		invalidate();//移动浮层到正确位置
		requestLayout();
	}
	
	//lxr
	public void pinfromAtMapWithMapCoord(PointF pointF) {
		fromMapCoord = pointF;
		fromScreenCoord = getScreenCoordinateFromMap(pointF);
		showfrom();//显示浮层
		invalidate();//移动浮层到正确位置
		requestLayout();
	}
	public void pingotoAtMapWithMapCoord(PointF pointF) {
		gotoMapCoord = pointF;
		gotoScreenCoord = getScreenCoordinateFromMap(pointF);
		showgoto();//显示浮层
		invalidate();//移动浮层到正确位置
		requestLayout();
	}
	//sitepoint
	public void sitepinAtMapWithMapCoord(PointF pointF,int i) {
		sitepiontMapCoord[i] = pointF;
		sitepointScreenCoord[i] = getScreenCoordinateFromMap(pointF);
		showsitepoint(i);//显示浮层
		setOverlayInfo();//设置并测量浮层栏
		invalidate();	//移动浮层到正确位置
		requestLayout();
	}
	
	//获得原图某点位置的商铺信息
	protected String getSpotInfoAtMapCoord(PointF mapCoord) {
		Map<String, String> spotInfo = drawView.getSpotNameAtPoint(overlayMapCoord);
		spotName = spotInfo.get(DrawView.SPOTINFO_NAME_KEY);
		currentSpotId = spotInfo.get(DrawView.SPOTINFO_ID_KEY);
		if (TextUtils.isEmpty(spotName)) {
				Log.d("wxq", "spotName:名字不存在！");
				return null;
		}
		Log.d("wxq", "spotName:"+spotName);		
		return spotName;
	}
	
	public String getspotName() {
		return spotName;
	}
	public String getCurrentSpotID() {
		return currentSpotId;
	}
	public PointF getOverlayMapCoord() {
		return overlayMapCoord;
	}
	
	//隐藏浮层图标
	public void hideOverlay() {
		overlayIV.setVisibility(View.INVISIBLE);
		overlayPopupView.setVisibility(View.INVISIBLE);
	}
	public void hideOverlay2() {		
		overlayPopupView2.setVisibility(View.INVISIBLE);
	}
	public void hideroutepiont() {
		routegoto.setVisibility(View.INVISIBLE);
		routefrom.setVisibility(View.INVISIBLE);
	}
	
	public void hidewcpoint() {
	for(int i=0;i<num;i++){
		sitepoint[i].setVisibility(View.INVISIBLE);
		}
	}
	public void hidemenu() {
		if(overmenuView.getVisibility() == View.VISIBLE)
		overmenuView.setVisibility(View.INVISIBLE);
	}
	

	
	public void hideroute() {
		if(routeplaybutton.getVisibility() == View.VISIBLE)
			routeplaybutton.setVisibility(View.INVISIBLE);
		routeView.hideRoute();
	}
	
	public void hiderouteinfo() {
		if(routeinfo.getVisibility() == View.VISIBLE)
			routeinfo.setVisibility(View.INVISIBLE);
	}
	
	public void hideAll() {
		hideOverlay();
		hideOverlay2();
		hidewcpoint();
		hidemenu();
		hideroute();
		hiderouteinfo();
		hideroutepiont();
	}
	//移动浮层
	protected void overlayScrollByDistance(float distanceX, float distanceY) {
		overlayScreenCoord.x -= distanceX;
		overlayScreenCoord.y -= distanceY;
		
		overlayScreenCoord2.x -= distanceX;
		overlayScreenCoord2.y -= distanceY;
		
		for(int i=0;i<num;i++){
		sitepointScreenCoord[i].x -=distanceX;
		sitepointScreenCoord[i].y -= distanceY;
		}
		routeinfoScreenCoord.x -= distanceX;
		routeinfoScreenCoord.y -= distanceY;
		
		fromScreenCoord.x -= distanceX;
		fromScreenCoord.y -= distanceY;
		
		gotoScreenCoord.x -= distanceX;
		gotoScreenCoord.y -= distanceY;
		
		invalidate();
		requestLayout();
	}

	//---------------缩放操作-------------------------------------------
	public void zoomIn() {
		PointF pF=new PointF();
		pF.x=this.getMeasuredWidth()/2;		pF.y=this.getMeasuredHeight()/2;
		PointF pF2=this.getMapCoordinateFromScreen(pF);
		drawView.zoomIn();
		routeView.zoom(drawView.scale);
		//hiderouteinfo();
		zoom(pF2);
	}
	
	public void zoomOut() {
		PointF pF=new PointF();
		pF.x=this.getMeasuredWidth()/2; 	pF.y=this.getMeasuredHeight()/2;
		PointF pF2=this.getMapCoordinateFromScreen(pF);
		drawView.zoomOut();
		routeView.zoom(drawView.scale);
		//hiderouteinfo();
		zoom(pF2);
	}
	
	protected void zoom(PointF pF2) {
		overlayScreenCoord = getScreenCoordinateFromMap(overlayMapCoord);
		for(int i=0;i<num;i++){
			sitepointScreenCoord[i]= getScreenCoordinateFromMap(sitepiontMapCoord[i]);
		}
		fromScreenCoord = getScreenCoordinateFromMap(fromMapCoord);
		gotoScreenCoord = getScreenCoordinateFromMap(gotoMapCoord);
		overlayScreenCoord2 =  getScreenCoordinateFromMap(overlayMapCoord2);
		routeinfoScreenCoord =  getScreenCoordinateFromMap(routeinfoMapCoord);
		
		scrolltopoint(pF2);
//		缩放后更新浮层位置。
		//invalidate();
		//requestLayout();
	}
	
	public void sitepoint(float X,float Y) {
	PointF siteMapPF = new PointF(X, Y);
	pinAtMapWithMapCoord(siteMapPF);	
	scrolltopoint(siteMapPF);
	}
	
	public void wcpoint(List<Map<String,Object>> wclist) {
		hidewcpoint();
		
		for(int i=0;i<num;i++){
		     if(!wclist.get(i).containsKey("yes")){
		    	 sitepoint[i].setImageResource(R.drawable.da_marker_red);}
		     else{if(wclist.get(i).get("yes").equals("1")){
		    	 sitepoint[i].setImageResource(R.drawable.tasks);
		     }else{sitepoint[i].setImageResource(R.drawable.important);}
		    	 
		     }
			//sitepoint[i].setImageResource(image);
		}
		
		PointF pF=new PointF();
		for(int i=0;i<wclist.size()&&i<num;i++){
		float xx=Float.parseFloat(wclist.get(i).get("Coord_X").toString());
		float yy=Float.parseFloat(wclist.get(i).get("Coord_Y").toString());
		pF = new PointF(xx,yy);
		sitepinAtMapWithMapCoord(pF,i);
		}
		scrolltopoint(pF);
	}
	public void DrawRoute(List<Map<String,Object>> point,String floor){
		routeView.DrawRoute(point,floor);
		if(routeplaybutton.getVisibility() != View.VISIBLE)
		{routeplaybutton.setVisibility(View.VISIBLE);}
		invalidate();	//移动浮层到正确位置
		requestLayout();
	}
	public void DrawRoute(String floor){
		routeView.changefloor(floor);
		if(routeplaybutton.getVisibility() != View.VISIBLE)
		{routeplaybutton.setVisibility(View.VISIBLE);}
		invalidate();	//移动浮层到正确位置
		requestLayout();
	}
	public String nextRoutePiont(){
		if(!routeView.piontlist.isEmpty()){
	    	if(currentpiont<=-1)
	    		currentpiont=0;
	     	else if(currentpiont>=routeView.piontlist.size()-1)
	     		return "ending";
	    	else currentpiont++;
		}
		return routeView.piontlist.get(currentpiont).get("FLOOR").toString();
	}
	public String lastRoutePiont(){
		if(!routeView.piontlist.isEmpty()){
			  if(currentpiont<=-1)
			   	currentpiont=0;
			  else if(currentpiont==0)
				  return "starting";
			  else currentpiont--;
			}
		return routeView.piontlist.get(currentpiont).get("FLOOR").toString();
	}
	public void updatedrawtoroute(){
		routeView.scrollBypoint(drawView.xOffset, drawView.yOffset);
		routeView.zoom(drawView.scale);	
	}
	public void scrolltopoint(PointF pF){
	PointF siteScreenPF=getScreenCoordinateFromMap(pF);
	drawView.scrollByDistance(siteScreenPF.x-this.getMeasuredWidth()/2, siteScreenPF.y-this.getMeasuredHeight()/2);
	routeView.scrollByDistance(siteScreenPF.x-this.getMeasuredWidth()/2, siteScreenPF.y-this.getMeasuredHeight()/2);
	overlayScrollByDistance(siteScreenPF.x-this.getMeasuredWidth()/2, siteScreenPF.y-this.getMeasuredHeight()/2);
	}
	public void changemap(String filename,String floor){
		drawView.changedbmap(filename,floor);
		 //invalidate();
		// requestLayout();
		 PointF pF=new PointF();
		if(filename.equals("phone3")) {
		 pF.x=332;pF.y=588;
	    }else{
		 pF.x=39;pF.y=32;}
		 scrolltopoint(pF);
	}
}
